Math.easeInOutQuad = function(t, b, c, d) {
  t /= d / 2
  if (t < 1) {
    return c / 2 * t * t + b
  }
  t--
  return -c / 2 * (t * (t - 2) - 1) + b
}

function scrollTo(element, to, duration) {
  const start = element.scrollTop
  const change = to - start
  const increment = 20
  let currentTime = 0

  function animateScroll() {
    currentTime += increment
    const val = Math.easeInOutQuad(currentTime, start, change, duration)
    element.scrollTop = val
    if (currentTime < duration) {
      setTimeout(animateScroll, increment)
    }
  }
  animateScroll()
}

export function scrollToTop() {
  const el = document.documentElement
  scrollTo(el, 0, 500)
}

export default {
  scrollTo,
  scrollToTop
}